package com.xinxing.learning.datasource.context;

import com.xinxing.learning.datasource.constants.DataSourceConstants;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DynamicDataSourceContextHolder {
    private static final ThreadLocal<String> DATASOURCE_CONTEXT_KEY_HOLDER = new ThreadLocal<>();

    public static void setContextKey(String key) {
        log.info("切换数据源" + key);
        DATASOURCE_CONTEXT_KEY_HOLDER.set(key);
    }

    public static String getContextKey() {
        return DATASOURCE_CONTEXT_KEY_HOLDER.get() != null ? DATASOURCE_CONTEXT_KEY_HOLDER.get() : DataSourceConstants.DS_KEY_MASTER;
    }

    public static void removeContextKey() {
        DATASOURCE_CONTEXT_KEY_HOLDER.remove();
    }
}
